package top.yuki.event;

import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.ItemStack;
import top.yuki.LevelWorld;
import top.yuki.data.DataManager;
import top.yuki.data.DayDataManager;
import top.yuki.data.TempDataManage;
import top.yuki.data.WorldManage;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Inventory implements Listener {
    public DataManager dataManage;
    private TempDataManage tempDataManage;
    private DayDataManager dayDataManager;
    private WorldManage worldManage;
    private ConfigurationSection config;
    private LevelWorld lw = LevelWorld.instance;
    private Pattern r = Pattern.compile("(\\d)级世界背包");
    public Inventory(LevelWorld lw) {
        this.dataManage = lw.dataManage;
        this.tempDataManage = lw.tempDataManage;
        this.dayDataManager = lw.dayDataManager;
        this.worldManage = lw.worldManage;
        this.config = lw.getConfig();

    }

    @EventHandler
    public void inventoryClose(InventoryCloseEvent e) {
        Player p = (Player) e.getPlayer();
        String title = e.getInventory().getTitle();
        String name = e.getInventory().getName();
        int worldLevel = 0;

        Matcher matcher = r.matcher(title);
        if (matcher.find()) {
            worldLevel = Integer.parseInt(matcher.group(1));
            File f = new File(lw.getDataFolder()+File.separator+"worldData"+File.separator+worldLevel+File.separator+p.getName() + ".yml");
            FileConfiguration c = YamlConfiguration.loadConfiguration(f);
            ItemStack[] closeContents = e.getInventory().getContents();
            ItemStack[] newCloseContents = closeContents;
            if (closeContents.length > 41) {
                newCloseContents = new ItemStack[41];
                System.arraycopy(closeContents, 0, newCloseContents, 0, 41);
            }
            c.set("inventory.content", newCloseContents);
            try {
                c.save(f);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

    }

    @EventHandler
    public void inventoryClickEvent(InventoryClickEvent e) {

        String title = e.getInventory().getTitle();
        Matcher matcher = r.matcher(title);
        if (!matcher.find()){return;}
        String clickedTitle = e.getClickedInventory().getTitle();
            if (clickedTitle != title) {
                if (e.getAction() == InventoryAction.COLLECT_TO_CURSOR ||
                        e.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY ||
                        e.getAction() == InventoryAction.PICKUP_SOME ||
                        e.getAction() == InventoryAction.PICKUP_ONE ||
                        e.getAction() == InventoryAction.PICKUP_HALF ||
                        e.getAction() == InventoryAction.PICKUP_ALL ||
                        e.getAction() == InventoryAction.SWAP_WITH_CURSOR ||
                        e.getAction() == InventoryAction.HOTBAR_SWAP ||
                        e.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD
                ) {
                    e.setCancelled(true);
                }
            } else {
                if (e.getAction() == InventoryAction.HOTBAR_SWAP ||
                        e.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD
                ) {
                    e.setCancelled(true);
                }

            }



    }
}







